//
//  ExtensionTableViewCell.m
//  19-Homework
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ExtensionTableViewCell.h"
#import "ExtensionList.h"
#import "ExtensionCollectionViewCell.h"
#define kExtenCollectionViewReuse @"extenReuse"
@interface ExtensionTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (nonatomic, strong) NSMutableArray<ExtensionList *> *listArray;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@end

@implementation ExtensionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.listArray = [NSMutableArray array];
        [self handleJSON];
        [self creatCollectionView];
    }
    return self;
}

- (void)handleJSON {
    NSURL *url = [NSURL URLWithString:@"http://223.99.255.20/adnewnc.app.autohome.com.cn/autov5.7.0/ad/infoad.ashx?version=6.2.0&platform=2&appid=2&networkid=0&adtype=1&provinceid=210000&cityid=0&lng=121.55074&lat=38.889747&gps_city=210200&pageid=48f7125f-05d5-46da-9364-818b45506ceb&isretry=0&deviceid=867702029633891"];
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
    mutableRequest.HTTPMethod = @"GET";
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{

        NSDictionary *dicJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = [dicJSON objectForKey:@"result"];
        NSArray *listArray = [resultDic objectForKey:@"list"];
        for (NSDictionary *listDic in listArray) {
            ExtensionList *extensionList = [ExtensionList extensionListWithDictionary:listDic];
            [_listArray addObject:extensionList];
        }
            [_myCollectionView reloadData];
        });
    }];
    [dataTask resume];
}

- (void)creatCollectionView {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeZero;
    _flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_myCollectionView];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    [_myCollectionView registerNib:[UINib nibWithNibName:@"ExtensionCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kExtenCollectionViewReuse];
}

- (void)layoutSubviews {
    _flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.contentView.bounds) - 40) / 3, CGRectGetHeight(self.contentView.bounds) - 10);
    _myCollectionView.frame = self.contentView.bounds;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ExtensionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kExtenCollectionViewReuse forIndexPath:indexPath];
    cell.extensionList = _listArray[indexPath.row];
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
